"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var types_1 = require("./types");
var util_1 = require("./util");
var lodash_1 = require("lodash");
function printBoolItem(exp) {
    var _a;
    var key = exp.key, value = exp.value;
    var left = 0;
    var right = 0;
    if (Array.isArray(value) && value.length > 1) {
        _a = value, left = _a[0], right = _a[1];
    }
    switch (exp.operator) {
        case types_1.Operator.FULL:
            return 'FULL';
        case types_1.Operator.EMPTY:
            return 'EMPTY';
        case types_1.Operator.EQ:
            return key + " == " + value;
        case types_1.Operator.NEQ:
            return key + " != " + value;
        case types_1.Operator.IN:
            return key + " in " + JSON.stringify(lodash_1.sortBy(value)).replace(/,/g, ', ');
        case types_1.Operator.NOT_IN:
            return key + " not in " + JSON.stringify(lodash_1.sortBy(value)).replace(/,/g, ', ');
        case types_1.Operator.LT:
            return key + " < " + value;
        case types_1.Operator.GT:
            return key + " > " + value;
        case types_1.Operator.NGT:
            return key + " <= " + value;
        case types_1.Operator.NLT:
            return key + " >= " + value;
        case types_1.Operator.RIN:
            return key + " ~ [" + left + ", " + right + "]";
        case types_1.Operator.LO_IN_INNER:
            return key + " ~ (" + left + ", " + right + "]";
        case types_1.Operator.RO_IN_INNER:
            return key + " ~ [" + left + ", " + right + ")";
        case types_1.Operator.O_IN_INNER:
            return key + " ~ (" + left + ", " + right + ")";
        case types_1.Operator.SEGMENTS_INNER:
            return exp.value.map(printBoolItem).join(' || ');
        default:
            throw new Error("Invalid Operator " + exp.operator);
    }
}
/**
 * 打印一堆 && 关系的布尔项
 *
 * @param {BoolItem[]} list - 布尔项
 * @return {string} 可读的字符串
 */
function printBoolItemList(list) {
    if (list.length === 1) {
        return printBoolItem(list[0]);
    }
    return list.map(function (item) { return "(" + printBoolItem(item) + ")"; }).join(' && ');
}
exports.printBoolItemList = printBoolItemList;
/**
 * 打印表达式
 *
 * @param {Expression} exp - 表达式
 * @return {string} 字符串格式， 空字符串表示全集，null表示空集
 */
function print(exp) {
    if (util_1.isBoolExpression(exp)) {
        var op = exp.operator === types_1.BoolOperation.AND ? ' && ' : ' || ';
        return exp.args.map(function (item) { return "(" + print(item) + ")"; }).join(op);
    }
    else {
        return printBoolItem(exp);
    }
}
exports.print = print;
